<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/8/1
 * Time: 11:22
 */
class DemoModel
{
    public function __construct()
    {
        $this->db = Yaf_Registry::get('db');
    }

    //获取用户信息
    public function getUserInfo($user_id)
    {
        $sql = "select * from user as u left join level as l on u.level=l.id where u.user_id=" . $userid;
        return $this->db->query($sql);
    }

    //获取购买枚数
    public function getMei($user_id)
    {
        $sql = "select sum(order_count) as meicount from `order` where userid = " . $user_id . " and status=1";
        return $this->db->query($sql);
    }

    //根据条件查询资产动态信息
    public function selectAsset($user_id, $postSelect, $page, $pagesize = 20)
    {


        $dateStart = strtotime($postSelect[dateTime]);
        $dateEnd = strtotime($postSelect[dataEnd]);

        $page = $page < 1 ? 1 : $page;
        $myoffset = ($page - 1) * $pagesize;

        $sql = "select * from asset where user_id = $user_id";
        if (!empty($postSelect[cash])) {
            $sql .= " and type = '$postSelect[cash]'";
        }
        if (!empty($postSelect[benefit])) {
            if (!empty($postSelect[cash])) {
                $sql .= " or type = '$postSelect[benefit]'";
            }else{
                $sql .= " and type = '$postSelect[benefit]'";
            }  
        }
        if (!empty($dateStart)) {
            $sql .= " and datatime > '$dateStart'";
        }
        if (!empty($dateEnd)) {
            $sql .= " and datatime < '$dateEnd'";
        }

        $sql .= " limit $myoffset,$pagesize";
        return $this->db->query($sql);
    }

    /* author dichaofeng
     * 分页显示
     * 获取资产总共条数
     */
    public function selectCount($user_id)
    {
        $sql = "select count(*) as count from asset where user_id = $user_id";
        $result = $this->db->get_one($sql);
        return (int)(isset($result['count']) ? $result['count'] : 0);
    }

    public function selectScreeningCount($user_id, $postSelect)
    {
        $dateStart = strtotime($postSelect[dateTime]);
        $dateEnd = strtotime($postSelect[dataEnd]);

        $sql = "select * from asset where user_id = $user_id";
        if (!empty($postSelect[cash])) {
            $sql .= " and type = '$postSelect[cash]'";
        }
        if (!empty($postSelect[benefit])) {
            if (!empty($postSelect[cash])) {
                $sql .= " or type = '$postSelect[benefit]'";
            }else{
                $sql .= " and type = '$postSelect[benefit]'";
            }  
        }
        if (!empty($dateStart)) {
            $sql .= " and datatime > '$dateStart'";
        }
        if (!empty($dateEnd)) {
            $sql .= " and datatime < '$dateEnd'";
        }

        $result = $this->db->get_one($sql);
        return (int)(isset($result['count']) ? $result['count'] : 0);
    }

    //添加备注信息
    public function updataRemark($post)
    {
        $sql = "update asset set remark = '$post[beizhu]' where serial_num = $post[liushuihao]";
        return $this->db->query($sql);
    }

    //进入页面时获取资产动态信息
    public function selectInfo($user_id, $page, $pagesize = 20)
    {
        $page = $page < 1 ? 1 : $page;
        $myoffset = ($page - 1) * $pagesize;
        $sql = "select * from asset where user_id = $user_id limit $myoffset,$pagesize";
        //print_r($sql);
        return $this->db->query($sql);
    }

    //进入页面时获取提现记录
    public function get_list($user_id, $page, $pagesize = 20)
    {
        $page = $page < 1 ? 1 : $page;
        $myoffset = ($page - 1) * $pagesize;
        $sql = "select * from asset where type=2 and user_id = $user_id limit $myoffset,$pagesize";
        //print_r($sql);
        return $this->db->query($sql);
    }
    /* author dichaofeng
     * 分页显示
     * 获取资产总共条数
     */
    public function get_list_count($user_id)
    {
        $sql = "select count(*) as count from asset where type=2 and user_id = $user_id";
        $result = $this->db->get_one($sql);
        return (int)(isset($result['count']) ? $result['count'] : 0);
    }
    /* author dichaofeng
   * 插入
   * 操作
   */
    public function setCash($data,$table="asset")
    {

        $keys=join(',',array_keys($data));
        $values="'".join("','", array_values($data))."'";
        $sql="insert {$table}({$keys}) VALUES ({$values})";

        $result = $this->db->get_one($sql);
        return $result;

    }
    /* author dichaofeng
   * 更新
   * 操作
   */
    public function updateCash($data,$where,$table="asset")
    {

        if(is_array($data)){
            $sqldata="";
            $i=0;
            foreach($data as $k=>$v){
                if($i==count($data)-1){
                    $sqldata.="$k='".$v."'";
                }else{
                    $sqldata.="$k='".$v."',";
                }
                $i++;
            }
        }
        $wheresql="where 1  ";
        if(is_array($where)){
            foreach($where as $k=>$v){
                if($v){
                    if($v<>"%%"){
                        $wheresql.="and $k='".$v."' ";
                    }

                }
            }
        }
        $sql="update $table set $sqldata $wheresql";
        $result = $this->db->get_one($sql);
        return $result;

    }
	/*
	*更新用户余额
	*/
    public function updateUser($data,$where,$table="asset")
    {
        $newdata=array();
        foreach($data as $k=>$v){
            $newdata[]=$k."=".$v."";
        }
        $sqldata=join(',',array_values($newdata));
        
        $newwhere=array();
        foreach($where as $k=>$v){
              $newwhere[]=$k."='".$v."'";
        }
        $sqlwhere=join(',',array_values($newwhere));

        $sql="update {$table} set {$sqldata} where 1 and {$sqlwhere}";
//print_r($sql);
        $result = $this->db->get_one($sql);
        return $result;
    }
	
} 
